#include <stdio.h>
#include <stdlib.h>
#include <cJSON.h>

static cJSON* imp_config_parse(char* filename)
{
    long len;
    char* p;
    int cnt;
    cJSON *json;;
    FILE* fp = fopen(filename, "r");
    if (!fp) {
        return NULL;
    }
    fseek(fp,0,SEEK_END);
    len=ftell(fp);
    if (0 == len) {
        return NULL;
    }
    fseek(fp,0,SEEK_SET);
    p = (char*) malloc(sizeof(char)*len);
    cnt = fread(p, len, 1, fp);
    fclose(fp);
    if (1 != cnt) {
        return NULL;
    }
    json=cJSON_Parse(p);
    if (!json) {
        return NULL;
    }
    free(p);
    return json;
}

static cJSON *config = NULL;

int imp_mem_dump = 0;

int imp_config_init(void) 
{
    char *p;
    cJSON *cJ;
    config = imp_config_parse("/tmp/impconfig.json");
    if (NULL == config) {
        printf("no imp config\n"); 
        return -1;
    }
    p = cJSON_Print(config);
    printf("%s\n", p);
    free(p);
    cJ = cJSON_GetObjectItem(config, "imp_mem_dump");
    imp_mem_dump = cJ->valueint;
    return 0;
}
